﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace NetJsWire.Lite.Helpers
{
    internal class RoundedRectangle
    {
        #region RectangleCorners enum

        [Flags]
        public enum RectangleCorners
        {
            None = 0, TopLeft = 1, TopRight = 2, BottomLeft = 4, BottomRight = 8,
            All = TopLeft | TopRight | BottomLeft | BottomRight
        }

        #endregion

        public static GraphicsPath Create( int x, int y, int width, int height,
                                          float radius, RectangleCorners corners )
        {
            var xw = x + width;
            var yh = y + height;
            var xwr = ( int ) ( xw - radius );
            var yhr = ( int ) ( yh - radius );
            var xr = ( int ) ( x + radius );
            var yr = ( int ) ( y + radius );
            var r2 = ( int ) ( radius * 2 );
            var xwr2 = xw - r2;
            var yhr2 = yh - r2;

            var p = new GraphicsPath();
            p.StartFigure();

            //Top Left Corner
            if ( ( RectangleCorners.TopLeft & corners ) == RectangleCorners.TopLeft )
            {
                p.AddArc( x, y, r2, r2, 180, 90 );
            }
            else
            {
                p.AddLine( x, yr, x, y );
                p.AddLine( x, y, xr, y );
            }

            //Top Edge
            p.AddLine( xr, y, xwr, y );

            //Top Right Corner
            if ( ( RectangleCorners.TopRight & corners ) == RectangleCorners.TopRight )
            {
                p.AddArc( xwr2, y, r2, r2, 270, 90 );
            }
            else
            {
                p.AddLine( xwr, y, xw, y );
                p.AddLine( xw, y, xw, yr );
            }

            //Right Edge
            p.AddLine( xw, yr, xw, yhr );

            //Bottom Right Corner
            if ( ( RectangleCorners.BottomRight & corners ) == RectangleCorners.BottomRight )
            {
                p.AddArc( xwr2, yhr2, r2, r2, 0, 90 );
            }
            else
            {
                p.AddLine( xw, yhr, xw, yh );
                p.AddLine( xw, yh, xwr, yh );
            }

            //Bottom Edge
            p.AddLine( xwr, yh, xr, yh );

            //Bottom Left Corner
            if ( ( RectangleCorners.BottomLeft & corners ) == RectangleCorners.BottomLeft )
            {
                p.AddArc( x, yhr2, r2, r2, 90, 90 );
            }
            else
            {
                p.AddLine( xr, yh, x, yh );
                p.AddLine( x, yh, x, yhr );
            }

            //Left Edge
            p.AddLine( x, yhr, x, yr );

            p.CloseFigure();
            return p;
        }

        public static GraphicsPath Create( Rectangle rect, int radius, RectangleCorners c )
        { return Create( rect.X, rect.Y, rect.Width, rect.Height, radius, c ); }

        public static GraphicsPath Create( int x, int y, int width, int height, float radius )
        { return Create( x, y, width, height, radius, RectangleCorners.All ); }

        public static GraphicsPath Create( Rectangle rect, float radius )
        { return Create( rect.X, rect.Y, rect.Width, rect.Height, radius ); }

        public static GraphicsPath Create( int x, int y, int width, int height )
        { return Create( x, y, width, height, 5 ); }

        public static GraphicsPath Create( Rectangle rect )
        { return Create( rect.X, rect.Y, rect.Width, rect.Height ); }
    }
}
